package org.aeon.client.view.components.listColumn;

import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.context.MessageMap;
import org.aeon.client.view.components.button.ContactButton;
import org.aeon.client.view.components.button.NotificationButton;
import org.aeon.client.view.components.button.TypeSelectionButton;
import org.aeon.client.view.components.sessionPanel.NotificationsPanel;
import org.aeon.client.view.components.sessionPanel.SessionPanel;
import org.aeon.constant.GroupDisplayType;
import org.aeon.constant.SessionType;
import org.aeon.entity.dto.ChatGroupDto;
import org.aeon.entity.dto.FriendApplyDto;
import org.aeon.entity.dto.UserDto;
import org.aeon.message.request.FriendApplyListRequestMessage;
import org.aeon.message.request.GroupsListRequestMessage;
import org.aeon.message.request.ListQueryRequestMessage;
import org.aeon.utils.SimpleRequestUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static org.aeon.client.context.MessageMap.MessageContextKey.*;


/**
 * @author: MissingAeon
 * @date: 2024/2/20 13:38
 * @description:
 */
@Slf4j
public class ContactListPanel extends JPanel {
    private final static String groupLabelText = "我创建的：";
    private final static String groupLabelText2 = "我加入的：";
    private final SessionPanel sessionPanel;
    private final JPanel listP;
    private final JButton button1;
    private final JButton button2;
    private final JButton button3;
    private final JButton button4;
    private final JButton button5;
    private final ColumnScrollPanel friendScroll;
    private final ColumnScrollPanel blacklistScroll;
    private final JLabel groupLabel;
    private final JLabel groupLabel2;
    private final ColumnScrollPanel groupScroll;
    private final ColumnScrollPanel groupScroll2;

    public ContactListPanel(SessionPanel sessionPanel) {
        this.sessionPanel = sessionPanel;
        this.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        button1 = new NotificationButton("好友通知");
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 3;
        constraints.insets = new Insets(5, 5, 1, 5);
        this.add(button1, constraints);

        button2 = new NotificationButton("群聊通知");
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 3;
        constraints.insets = new Insets(1, 5, 5, 5);
        this.add(button2, constraints);


        button3 = new TypeSelectionButton("好友");
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.insets = new Insets(0, 5, 5, 0);
        this.add(button3, constraints);

        button4 = new TypeSelectionButton("黑名单");
        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.insets = new Insets(0, 1, 5, 1);
        this.add(button4, constraints);

        button5 = new TypeSelectionButton("群聊");
        constraints.gridx = 2;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.insets = new Insets(0, 0, 5, 5);
        this.add(button5, constraints);

        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 3;
        constraints.insets = new Insets(0, 0, 0, 0);

        final CardLayout layout = new CardLayout();
        listP = new JPanel(layout);
        final Dimension dimension = new Dimension(296, 520);
        friendScroll = new ColumnScrollPanel(dimension);
        listP.add("UL", friendScroll);
        blacklistScroll = new ColumnScrollPanel(dimension);
        listP.add("BL", blacklistScroll);

        final JPanel groupPanel = new JPanel();
        groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS));
        // 创建一个带有内边距的空边框
        Border paddingBorder = BorderFactory.createEmptyBorder(5, 20, 0, 0);

        final Box groupLabelBox = Box.createHorizontalBox();
        // 将边框应用于标签
        groupLabelBox.setBorder(paddingBorder);

        groupLabel = new JLabel(groupLabelText);
        groupLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        groupLabelBox.add(groupLabel);
        groupLabelBox.add(Box.createHorizontalGlue());
        groupPanel.add(groupLabelBox);

        final Dimension dimension2 = new Dimension(296, 240);
        groupScroll = new ColumnScrollPanel(dimension2);
        groupPanel.add(groupScroll);

        final Box groupLabelBox2 = Box.createHorizontalBox();
        groupLabelBox2.setBorder(paddingBorder);
        groupLabel2 = new JLabel(groupLabelText2);
        groupLabel2.setAlignmentX(Component.RIGHT_ALIGNMENT);
        groupLabelBox2.add(groupLabel2);
        groupLabelBox2.add(Box.createHorizontalGlue());
        groupPanel.add(groupLabelBox2);

        groupScroll2 = new ColumnScrollPanel(dimension2);
        groupPanel.add(groupScroll2);

        listP.add("GL", groupPanel);

        this.add(listP, constraints);

        initListener();
    }

    private void initListener() {
        final GlobalContext context = GlobalContext.getContext();
        button1.addActionListener(e -> {
            final FriendApplyListRequestMessage reqMsg = new FriendApplyListRequestMessage();
            reqMsg.setMessage(context.getCurrentUser().getId());
            SimpleRequestUtil.sendAndWait(context, reqMsg, () -> {
                SwingUtilities.invokeLater(() -> {
                    LinkedHashMap<FriendApplyDto, UserDto> map = context.getMessageMap().del(FriendConfirmationList);
                    final NotificationsPanel panel = new NotificationsPanel();
                    panel.setTitle("好友通知");
                    panel.fillInData(map);
                    sessionPanel.add(panel);
                });
            });
        });
        button2.addActionListener(e -> {
            final NotificationsPanel panel = new NotificationsPanel();
            panel.setTitle("群聊通知");
            sessionPanel.add(panel);
        });

        final CardLayout layout = (CardLayout) listP.getLayout();

        final MessageMap messageMap = context.getMessageMap();

        // 用户按钮处理方式
        final Runnable runnable = () -> {
            // 展示
            final List<UserDto> list = messageMap.del(MemberList);
            list.forEach(item -> {
                ContactButton button = new ContactButton(item.getNickname(), sessionPanel);
                if (item.isLeave()) {
                    button.setColorFade();
                }
                button.setAdditionalInfo(item.getId(), SessionType.USER_SESSION);
                friendScroll.addItem(button);
            });
        };

        button3.addActionListener(e -> {
            friendScroll.clean();
            layout.show(listP, "UL");
            final ListQueryRequestMessage reqMsg = new ListQueryRequestMessage(context.getCurrentUser().getId(), true);
            SimpleRequestUtil.sendAndWait(context, reqMsg, () -> SwingUtilities.invokeLater(runnable));

        });

        button4.addActionListener(e -> {
            blacklistScroll.clean();
            layout.show(listP, "BL");
            final ListQueryRequestMessage reqMsg = new ListQueryRequestMessage(context.getCurrentUser().getId(), false);
            SimpleRequestUtil.sendAndWait(context, reqMsg, () -> SwingUtilities.invokeLater(runnable));
        });

        button5.addActionListener(e -> {
            groupScroll.clean();
            groupScroll2.clean();
            layout.show(listP, "GL");
            final GroupsListRequestMessage reqMsg = new GroupsListRequestMessage(context.getCurrentUser().getId());
            SimpleRequestUtil.sendAndWait(context, reqMsg, () -> SwingUtilities.invokeLater(() -> {
                final Map<GroupDisplayType, List<ChatGroupDto>> map = messageMap.del(GroupMapList);
                // 群聊按钮处理方式
                Function<ChatGroupDto, Component> function = (item) -> {
                    ContactButton button = new ContactButton(item.getGroupName(), sessionPanel);
                    button.setAdditionalInfo(item.getId(), SessionType.GROUP_SESSION);
                    return button;
                };
                map.get(GroupDisplayType.CREATED).forEach(group -> groupScroll.addItem(function.apply(group)));
                map.get(GroupDisplayType.JOINED).forEach(group -> groupScroll2.addItem(function.apply(group)));
            }));
        });
    }

}
